/**
 * 
 */
package com.nd.android.payment.sdk.dialog;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import com.common.StarAppConfig;
import com.nd.android.payment.R;
import com.nd.android.payment.ui.task.CommonBuyTask;
import com.nd.android.payment.ui.task.ICommonBuyCallBack;
import com.nd.android.payment.util.PaymentUtils;
import com.nd.commplatform.NdCallbackListener;
import com.nd.commplatform.NdCommplatform;
import com.nd.commplatform.NdCommplatformSdk;
import com.nd.commplatform.NdErrorCode;
import com.nd.commplatform.NdMiscCallbackListener;
import com.nd.commplatform.entry.NdBuyInfo;
import com.product.android.utils.LogUtils;

/**
 * 异步支付
 * <br>
 * Created 2014-11-14 下午3:13:22
 * 
 * @version
 * @author zhangqr
 * 
 * @see
 */

public class SDKDialogUtils {
    
    private final static String TAG = SDKDialogUtils.class.getSimpleName();
    
    /**
     * 打开异步支付弹窗
     * 
     * <br>Created 2014-11-14 下午3:27:15
     * @param activity  activity
     * @param amount    要购买的星钻数量
     * @param callBack  回调
     * @author       zhangqr
     */
    public static void openNdUniPayAsynDialog(final Activity activity, final int amount,
            final ICommonBuyCallBack callBack) {
        //创建订单
        final NdBuyInfo order = new NdBuyInfo();
        order.setSerial(PaymentUtils.getSerial(activity));
        order.setProductId(PaymentUtils.getProductId(activity));
        order.setProductName(PaymentUtils.getProductName(activity));
        order.setProductPrice(PaymentUtils.getProductPrice(activity));
        order.setCount(amount);

        NdCommplatformSdk.getInstance().initTransportAisle(activity,
                new NdCallbackListener<Object>() {
                    @Override
                    public void callback(int responseCode, Object t) {
                        if (responseCode == 0) {
                            final String token = PaymentUtils.getToken(activity);
                            final String account = PaymentUtils.getUserName();
                            if(! TextUtils.isEmpty(token)) {
                                NdCommplatform.getInstance().ndAutoLogin(account,
                                        token, activity,
                                        new NdCallbackListener<Object>() {

                                            @Override
                                            public void callback(int responseCode, Object t) {
                                                if (responseCode == 0) {
                                                    openNdUniPayAsynDialog(activity, order, callBack);
                                                } else {
                                                    Log.i(TAG, "SDK login fail : " + getResult());

                                                    //TODO 输出日志
                                                    LogUtils.writeLogToFile(activity,
                                                            " NDSDK : ndAutoLogin, responseCode = "  + responseCode
                                                            + ", getResult() = " + getResult());

                                                    if (callBack != null) {
                                                        callBack.onFinish();
                                                    }

                                                    PaymentUtils.sureToLogout(activity);
                                                }
                                            }
                                        });
                            } else {
                                //TODO 输出日志
                                //LogUtils.writeLogToFile(activity, " NDSDK : PaymentUtils.getToken() is empty");

                                if (callBack != null) {
                                    callBack.onFinish();
                                }

                                PaymentUtils.sureToLogout(activity);
                            }
                        } else {
                            //TODO 输出日志
                            LogUtils.writeLogToFile(activity, " NDSDK : initTransportAisle, responseCode = "  + responseCode);

                            if (callBack != null) {
                                callBack.onFinish();
                            }

                            PaymentUtils.sureToLogout(activity);
                        }
                    }
                });
    }
    
    private static void openNdUniPayAsynDialog(final Activity activity, final NdBuyInfo order,
            final ICommonBuyCallBack callBack) {
        //调用SDK异步支付弹窗
        int aError = NdCommplatform.getInstance().ndUniPayAsynDialog(order, activity,
                new NdMiscCallbackListener.OnPayProcessListener() {
                    
                    @Override
                    public void finishPayProcess(int code) {
                        String msg = "";
                        if (code == NdErrorCode.ND_COM_PLATFORM_SUCCESS) {
                            //新建任务，用于查询余额
                            new CommonBuyTask(activity, CommonBuyTask.FROM_BUY_DIAMOND, callBack).execute(order.getCount());
                            return;
                            
                        } else if (code == NdErrorCode.ND_COM_PLATFORM_ERROR_PAY_FAILURE) {
                            msg = activity.getString(R.string.PAY_NDERRORCODE_ND_COM_PLATFORM_ERROR_PAY_SESSION_TIMEOUT);
                        } else if (code == NdErrorCode.ND_COM_PLATFORM_ERROR_PAY_CANCEL) {
                            msg = activity.getString(R.string.PAY_NDERRORCODE_ND_COM_PLATFORM_ERROR_PAY_CANCEL);
                        } else if (code == NdErrorCode.ND_COM_PLATFORM_ERROR_PAY_ASYN_SMS_SENT) {
                            msg = activity.getString(R.string.PAY_NDERRORCODE_ND_COM_PLATFORM_ERROR_PAY_ASYN_SMS_SENT);
                        } else if (code == NdErrorCode.ND_COM_PLATFORM_ERROR_PAY_REQUEST_SUBMITTED) {
                            msg = activity.getString(R.string.PAY_NDERRORCODE_ND_COM_PLATFORM_ERROR_PAY_REQUEST_SUBMITTED);
                        } else {
                            msg = activity.getString(R.string.PAY_NDERRORCODE_OTHERS);

                            //TODO 输出日志
                            LogUtils.writeLogToFile(activity, " NDSDK : ndUniPayAsynDialog, code = "  + code + ", msg = " + msg);
                        }

                        if (callBack != null) {
                            callBack.onFail(msg);
                        }
                        
                        //TODO 测试开始
                        //注销登录
//                        NdCommplatform.getInstance().ndLogout(
//                                NdCommplatform.LOGOUT_TO_RESET_AUTO_LOGIN_CONFIG, activity);
                        //TODO 测试结束
                    }
                });
        if (aError != 0) {
            if (callBack != null) {
                callBack.onFail(activity.getString(R.string.PAY_NDERRORCODE_OTHERS));
            }
        }
    }
    
}
